<?php

/**
 * gallery actions.
 *
 * @package    sf_sandbox
 * @subpackage gallery
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class galleryActions extends autogalleryActions
{
    public function executeIndex()
    {
        $this->forward('gallery', 'list');
    }
    
    /**
     * Displays list of all galleries (by label PHOTOS or by parent `id`)
     */
    public function executeCatalog()
    {
        $parentId = (int)$this->getRequestParameter('id', 0);
        if( $parentId < 1 )
        {
            /* @var $tree Tree */
            $tree = Tree::getByLabel('PHOTOS');
            $this->forward404Unless($tree);
            $parentId = $tree->getId();
        }

        //$this->galleries = Gallery::getByParent($parentId);
        // Making sorting by updated_at, DIRTY!
        $ar = Gallery::getByParent($parentId);
        $arToSort = array();
        for( $i = 0; ($ar != null) && ($i < count($ar)); $i++ )
        {
            $arToSort[$ar[$i]->getUpdatedAt(null)] = $ar[$i];
        }
        krsort($arToSort);
        $this->galleries = $arToSort;
        
        $this->title = sfContext::getInstance()->getI18N()->__('shows');
    }
    

    public function executeDisplay()
    {
        // Check if we have ID of gallery to display
        $objId = (int)$this->getRequestParameter('id', 0);
        if( $objId < 1 )
        {
            /* @var $tree Tree */
            $tree = Tree::getByLabel('FUN PHOTOS');
            $this->forward404Unless($tree);
            $objId = $tree->getObjId();
        }

        $this->gallery = GalleryPeer::retrieveByPK($objId);
        $this->forward404Unless($this->gallery);
//        debugvar($this->gallery, 'Gallery');
        //$this->title = $tree->getName();

        $c =  new Criteria();
        $c->add(FilesPeer::OBJ_TYPE, MyConstants::TYPE_GALLERY);
        $c->add(FilesPeer::OBJ_ID, $objId);
//        $this->gallery = $objId;
        $c->addDescendingOrderByColumn(FilesPeer::PRIORITY);

        $this->pager = new sfPropelPager('Files', 24);
        $this->pager->setCriteria($c);
        $this->pager->setPage($this->getRequestParameter('p', 1));
        $this->pager->init();
    }

    public function executeEditinfo()
    {
        $this->image = FilesPeer::retrieveByPk($this->getRequestParameter('id'));
        $this->forward404Unless($this->image);

        if( $this->getRequest()->getMethod() == sfRequest::POST )
        {
            $this->image->setName($this->getRequestParameter('name'));
            $this->image->setText($this->getRequestParameter('text'));
            $this->image->save();
            $this->redirect('gallery/editinfo?id=' . $this->image->getId());
            //exit('saving...');

        }
    }

    public function executeShow()
    {
        //we need an ID to display gallery
        $parentId = $this->getRequestParameter('id');

        // if we don't have an ID the page must display the first gallery of tree labeled 'photos'
        if( !$parentId )
        {
            $this->arBranch = Tree::getTreeByLabel('Photos');
            $c = new Criteria();
            $c->add(TreePeer::PARENT_ID, $this->arBranch[0]['id']);
            $firstChild = TreePeer::doSelectOne($c);
            if( $firstChild )
            {
                $this->page = GalleryPeer::retrieveByPk($firstChild->getObjId());
                $galleryId = $this->page->getId();
            }

        }
        else
        {
            $this->arBranch = Tree::getTree($parentId);
            $this->page = GalleryPeer::retrieveByPk($this->getRequestParameter('id'));
            $galleryId = $parentId;
        }

        if( isset($galleryId) )
        {
            $c = new Criteria();
            $c->add(FilesPeer::OBJ_TYPE, MyConstants::TYPE_GALLERY);
            $c->add(FilesPeer::OBJ_ID, $galleryId);
            $pager = new sfPropelPager('Files', 20);
            $pager->setCriteria($c);
            $pager->setPage($this->getRequestParameter('page', 1));
            $pager->init();
            $this->pager = $pager;
        }


        if( $this->page )
        {
            $this->title = $this->page->getName();
            $image = Files::getFirst($this->page->getId());
            if( $image )
            {
                $this->image = $image->getUname();
                $this->imgPath = Files::getDirPath('tiny');
                $this->path = 'http://' . sfConfig::get('app_website') . '/' . sfConfig::get('sf_web_dir_name') . '/' . sfConfig::get('app_images_dir');
            }
        }
    }

    public function executePage()
    {
        $this->page = galleryPeer::retrieveByPk($this->getRequestParameter('id'));
        $this->page->setCulture($this->getUser()->getCulture());
        $this->title = $this->page->getName();
        $image = Files::getFirst($this->page->getId());
        if( $image )
        {
            $this->image = $image->getUname();
            $this->imgPath = Files::getDirPath('tiny');
            $this->path = 'http://' . sfConfig::get('app_website') . '/' . sfConfig::get('sf_web_dir_name') . '/' . sfConfig::get('app_images_dir');
        }
        $this->layout = new gallery();
        $c = new Criteria();
        $c->add(TreePeer::OBJ_ID, $this->page->getId());
        $obj = TreePeer::doSelectOne($c);
        $arParents = Tree::getBranchById($obj->getId(), $this->getUser()->getCulture());
        $this->path = 'Titulinis / ';
        foreach( $arParents as $parent )
        {
            if( $parent->getId() != 1 )
            {
                $this->path .= $parent->getName() . ' / ';
            }
        }
        $this->path .= $this->page->getName();
    }

    protected function getGalleryOrCreate($id = 'id')
    {
        /* @var $gallery Gallery */
        $gallery = parent::getGalleryOrCreate($id);
        $gallery->setCulture($this->getUser()->getCulture());
        return $gallery;
    }
    protected function updateGalleryFromRequest()
    {
        parent::updateGalleryFromRequest();
        $this->gallery->setStatusId(MyConstants::STATUS_NEW);
        if (isset($gallery['backlink']))
        {
            $this->gallery->setBacklink($gallery['backlink']);
        }
        if (isset($gallery['text_layout_id']))
        {
            $this->gallery->setTextLayoutId($gallery['text_layout_id'] ? $gallery['text_layout_id'] : null);
        }
        if (isset($gallery['backlink']))
        {
            $this->gallery->setBacklink($gallery['backlink']);
        }
    }

    public function executeRemovefile()
    {
        $uname = strip_tags($this->getRequestParameter('uname'));
        Files::deleteByUname(Files::getNameFromUrl($uname), true);
        $this->redirect('gallery/edit?id='.$this->getRequestParameter('id'));
    }
    public function executeEdit()
    {
        if( ($pageId = $this->getRequestParameter('pageId')) )
        {
            $sLink = '&pageId=' . $pageId;
        }
        parent::executeEdit();
    }

    protected function saveGallery($gallery)
    {
        parent::saveGallery($gallery);
        if( $this->getRequestParameter('idsort') )
        {
            $id_list = $this->getRequestParameter('idsort');
            $arId = explode('id[]=', $this->getRequestParameter('idsort'));
            // change priority
            $con = Propel::getConnection();
            
            for( $i = 0; $i < count($arId); $i++ )
            {
                if( $arId[$i] )
                {
                    $c1 = new Criteria();
                    $c1->add(FilesPeer::ID, (int)str_replace('&', '', $arId[$i]));
                     
                    // update set
                    $c2 = new Criteria();
                    $c2->add(FilesPeer::PRIORITY, count($arId) - $i);
                    $affectedUpdateRows = BasePeer::doUpdate($c1, $c2, $con);
                }
            }
        }
        if( $this->getRequestParameter('gallery[parent_id]') )
        {
            $parentId = $this->getRequestParameter('gallery[parent_id]');
        } else
        {
            $photosBranch = Tree::getByLabel('Photos');
            $parentId = $photosBranch->getId();
        }
        $options = array();
        //debugvar(MyConstants::getModuleTypeList());
        $options['moduleType'] = MyConstants::TYPE_GALLERY;
        $options['moduleId'] = $gallery->getId();
        if( !$this->getRequestParameter('id') )
        {
            Tree::create($this->getUser()->getAttribute('userId'), $parentId, $options);
        }
    }

    public function executeTest()
    {
    	$id = $this->getRequestParameter('d');
    	$c = new Criteria();
        $c->add(ProductPeer::ID, MyConstants::UNDEFINED_ID, Criteria::NOT_EQUAL);
        APriority::moveUp($c, $id);
    }

}
